YAML: Yet Another Markup Language
Table of Content
YAML: Yet Another Markup Language#
Or ain't markup language
YAML is a digestible data serialization language often used to create configuration files with any programming language.
The basic structure of a YAML file is a map. key: value
YAML use two spaces per level of indentation
minimal yaml file
version: 2
Demo: Read yaml file with python#
PyYAML
pip install PyYAML
import yaml
from pathlib import Path
import json
p = Path(__file__).with_name("data.yaml")
with p.open("r") as f:
data = yaml.load(f, Loader=yaml.FullLoader)
# Print pretty
print(json.dumps(data, indent=4))
VSCode#
ext install redhat.vscode-yaml
YAML Comments#
# comments: 2
YAML Strings#
team: 76ers
team1: "76ers"
python parse result
{
"team": "76ers",
"team1": "76ers"
}
YAML Numbers#
n1: 1000
n2: 10.100
python parse result
{
"n1": 1000,
"n2": 10.1
}
YAML Boolean#
bool: true
bool1: on
bool2: off
bool11: yes
bool22: no
python parse result
{
"bool": true,
"bool1": true,
"bool2": false,
"bool11": true,
"bool22": false
}
YAML Multi line Strings#
# Combine the multi line to one line in the output
multiple_line: >
line 1
line 2
line 2
# Add new line for each
multiple_line1: |
line 1
line 2
line 2
python parse result
{
"multiple_line": "line 1 line 2 line 2\n",
"multiple_line1": "line 1\nline 2\nline 2\n"
}
YAML List#
data_list:
- item1
- item2
data_list1: [item1, item2]
python parse result
{
"data_list": [
"item1",
"item2"
],
"data_list1": [
"item1",
"item2"
]
}
YAML Dictionary#
dic:
item1: 10
item2: hello
python parse result
{
"dic": {
"item1": 10,
"item2": "hello"
}
}
YAML Complex type#
list of objects
objects:
- name: name1
number: 1
is_bool: true
- name: name2
number: 3
is_bool: false
python parse result
{
"objects": [
{
"name": "name1",
"number": 1,
"is_bool": true
},
{
"name": "name2",
"number": 3,
"is_bool": false
}
]
}